import { defineStore } from "pinia";
import mockRequest from '@/api/mock'
import { showToast } from 'vant';
interface goodsItemData {
    uniqueId: string,
    checked: boolean,
    discountPrice: string,
    cnt: number,
    retailPrice: number,
    specList: {
        specValue: string
    },
    prefix: string,
    itemName: string,
    pic: string,
    bottomTips: [{
        value: string
    }]
}
interface cartItemGroupListData {
    groupTip: string,
    groupTitle: string,
    toGoTip: string
}
interface globalInfoVOData {
    showActivityPrice: string,
    showActualPrice: string,
    showTotalPrice: string
}
type addBuyPreviewItemListData = {
pic: string | undefined;
    uniqueId: string,
    itemName: string,
    prefix: string,
    priceTag: string,
    showActualPrice: string,
    showRetailPrice: string,
}
type addBuyPreviewData = {
    addBuyPreviewItemList: addBuyPreviewItemListData[]
}
interface cartGlobalGroupListData {
    promTip: string,
    promTitle: string,
    promotionTip: string,
    addBuyPreview: addBuyPreviewData,
}
type shopcartStoreState = {
    cartGlobalGroupList: cartGlobalGroupListData,
    globalInfoVO: globalInfoVOData,
    cartItemGroupList: cartItemGroupListData,
    goodsData: goodsItemData[]
}
export const useShopcartStore = defineStore('useShopcartStore', {
    state: (): shopcartStoreState => {
        return {
            cartGlobalGroupList: {
                promTip: '',
                promTitle: '',
                promotionTip: '',
                addBuyPreview: {
                    addBuyPreviewItemList: []
                },
            },
            globalInfoVO: {
                showActivityPrice: '',
                showActualPrice: '',
                showTotalPrice: ''
            },
            cartItemGroupList: {
                groupTip: '',
                groupTitle: '',
                toGoTip: ''
            },
            goodsData: []
        }
    },
    actions: {
        async getShopCartData() {
            try {
                const res = await mockRequest({ url: '/headerInfo', method: 'get' })
                console.log(res);
                this.cartItemGroupList = res.data.cartItemGroupList[0]
                this.goodsData = res.data.cartItemGroupList[0].promotionGroupList[0].cartItemList
                this.cartGlobalGroupList = res.data.cartGlobalGroupList[0]
                this.globalInfoVO = res.data.globalInfoVO
            } catch (error) {
                console.log(error);

                showToast('请求数据失败');
            }
        }

    },
    getters: {
        headerLine1(): cartGlobalGroupListData {
            return this.cartGlobalGroupList
        },
        recommendGoods(): addBuyPreviewItemListData[] {
            return this.cartGlobalGroupList.addBuyPreview?.addBuyPreviewItemList || []
        },
        goodsHeader(): cartItemGroupListData {
            return this.cartItemGroupList
        },
        goodsListData(): goodsItemData[] {
            return this.goodsData
        },
        priceData(): globalInfoVOData {
            return this.globalInfoVO
        }
    }
})